﻿
Imports std

'parameter_list = value_clause{,value_clause}
Public Class parameters_list
    Inherits iScript
    Implements iParameters, iParametersBetweenContext
    Public Const separator As Char = character.comma
    Public Const defaultOverwrite As Boolean = True
    Public Shared ReadOnly empty As parameters_list
    Private ps As vector(Of value_clause) = Nothing

    Shared Sub New()
        empty = New parameters_list(New vector(Of value_clause)())
    End Sub

    Protected Friend Shared Function _isMeaningfulMark(ByVal c As Char) As Boolean
        Return isseparator(c)
    End Function

    Private Sub New(ByVal ips As vector(Of value_clause))
        ps = ips
    End Sub

    Private Function _invoke(ByVal dest As context, ByVal src As context, _
                             ByVal overwrite As Boolean) As Boolean
        assert(Not dest Is Nothing, "destination context is nothing.")
        assert(Not src Is Nothing, "source context is nothing.")

        Dim i As Int64
        For i = 0 To ps.size() - 1
            If Not ps(i).invoke(dest, src, overwrite) Then
                Return False
            End If
        Next

        Return True
    End Function

    Protected Overrides Function invokeImpl(Of T)(ByVal dest As context, ByVal src As context, _
                                                  ByRef overwrite As T) As Boolean
        Return _invoke(dest, src, convertor.cast(Of Boolean)(overwrite))
    End Function

    Public Function invoke(ByVal dest As context, ByVal src As context, ByVal overwrite As Boolean) As Boolean
        Return invokebase(dest, src, overwrite)
    End Function

    Public Function invoke(ByVal dest As context, ByVal src As context) As Boolean _
                                                                       Implements iParametersBetweenContext.invoke
        Return invoke(dest, src, defaultOverwrite)
    End Function

    Public Function invoke(ByVal ctx As context, ByVal overwrite As Boolean) As Boolean
        Return invoke(ctx, ctx, overwrite)
    End Function

    Public Function invoke(ByVal ctx As context) As Boolean Implements iParameters.invoke
        Return invoke(ctx, defaultOverwrite)
    End Function

    Private Shared Function isseparator(ByVal c As Char) As Boolean
        Return c = separator
    End Function

    Protected Shared Function parseImpl(ByVal input As String, ByRef index As Int64, _
                                        ByRef output As parameters_list) As Boolean
        Dim vcs As vector(Of value_clause) = Nothing
        vcs = New vector(Of value_clause)()
        Dim vc As value_clause = Nothing
        If value_clause.parse(input, index, vc) Then
            vcs.push_back(vc)
            While token(input, index, Nothing, AddressOf isseparator, AddressOf [false])
                If value_clause.parse(input, index, vc) Then
                    vcs.push_back(vc)
                Else
                    Return False
                End If
            End While
        End If

        output = New parameters_list(vcs)
        Return True
    End Function

    Public Shared Function parse(ByVal input As String, ByRef index As Int64, _
                                 ByRef output As parameters_list) As Boolean
        Return parsebase(input, index, output, AddressOf parseImpl)
    End Function

    Public Overrides Function ToString() As String
        If ps Is Nothing Then
            Return Nothing
        Else
            Return ps.ToString(separator)
        End If
    End Function
End Class
